package cn.sq.musicserver.plug.model.wpmusicapi.kg;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.sq.musicserver.exception.PlugError;
import cn.sq.musicserver.plug.model.wpmusicapi.constants.WPMusicApiConstantKG;
import cn.sq.musicserver.plug.model.wpmusicapi.kg.entity.APIDownloadParam;
import cn.sq.musicserver.plug.model.wpmusicapi.kg.entity.APIDownloadResult;
import cn.sq.musicserver.plug.model.wpmusicapi.kg.entity.APISearchParam;
import cn.sq.musicserver.plug.model.wpmusicapi.kg.entity.APISearchResult;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: SQ
 * Date: 2021/1/6
 * Time: 10:44
 * Description:
 */
@Slf4j
@Component
public class WPKGMusicApiService {

    /**
     * 搜索音乐
     *
     * @param apiSearchParam 搜索参数
     * @return 搜索结果
     * @throws IOException
     */
    public APISearchResult search(APISearchParam apiSearchParam) throws PlugError {

        Connection connect = Jsoup.connect(WPMusicApiConstantKG.SEARCH);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);
        if (StringUtils.isEmpty(apiSearchParam.getKey())) {
            throw new PlugError("请填写搜索内容");
        }
        connect.data("key", apiSearchParam.getKey());
        if (StringUtils.isNotEmpty(apiSearchParam.getOffset())) {
            connect.data("offset", apiSearchParam.getOffset());
        }
        if (StringUtils.isNotEmpty(apiSearchParam.getLimit())) {
            connect.data("limit", apiSearchParam.getLimit());
        }
//        if (StringUtils.isNotEmpty(apiSearchParam.getType())) {
//            connect.data("type", apiSearchParam.getType());
//        } else {
//            connect.data("type", "0");
//        }
        Document document = null;
        try {
            document = connect.timeout(100000).get();
        } catch (IOException e) {
            log.error("插件运行异常："+e.getMessage());
            throw new PlugError("插件运行io异常");
        }
        String text = document.body().text();
        APISearchResult apiSearchResult = JSONUtil.toBean(text, APISearchResult.class);
//        apiSearchResult.getData().getLists().forEach(iteam->{
//            iteam.setImgurl(download(new APIDownloadParam().setAid(iteam.getAlbumID()).setHash(iteam.getFileHash())).getData().getImg());
//        });
        return apiSearchResult;
//        return new Gson().fromJson(text, APISearchResult.class);
    }




    /**
     * 下载音乐
     *
     * @param apiDownloadParam 下载参数  hash传空则只进行爬取歌曲信息无法获取下载链接
     * @return 下载地址
     * @throws IOException
     */
    public APIDownloadResult download(APIDownloadParam apiDownloadParam) throws PlugError {
        Connection connect = Jsoup.connect(WPMusicApiConstantKG.DOWNLOAD);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);

//        if(StringUtils.isEmpty(apiDownloadParam.getAid())||StringUtils.isEmpty(apiDownloadParam.getMid())){
//            throw new PlugError("请填写下载相关参数");
//
//        }
        if (StringUtils.isNotEmpty(apiDownloadParam.getAid())){
            connect.data("aid", apiDownloadParam.getAid());
        }else{
//            throw new PlugError("请填写下载音乐id");
        }
        connect.data("hash", apiDownloadParam.getHash());


//        if (StringUtils.isNotEmpty(apiDownloadParam.getHash())) {
//            connect.data("br", apiDownloadParam.getBr());
//        } else {
//            throw new PlugError("请填写下载音乐清晰度");
//        }
        try {
            return new Gson().fromJson(connect.get().body().text(), APIDownloadResult.class);
        } catch (IOException e) {

            log.error("插件运行异常："+e.getMessage());
            throw new PlugError("插件运行io异常");
        }
    }

    /**
     * 歌词
     *
     * @param apiDownloadParam 歌词参数（和下载一样使用同一个接口）
     * @return 歌词信息
     * @throws IOException
     */
    public APIDownloadResult lyric(APIDownloadParam apiDownloadParam) throws PlugError {
        Connection connect = Jsoup.connect(WPMusicApiConstantKG.DOWNLOAD);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);

//        if(StringUtils.isEmpty(apiDownloadParam.getAid())||StringUtils.isEmpty(apiDownloadParam.getMid())){
//            throw new PlugError("请填写下载相关参数");
//
//        }
        if (StringUtils.isNotEmpty(apiDownloadParam.getAid())){
            connect.data("aid", apiDownloadParam.getAid());
        }else{
            throw new PlugError("请填写下载音乐id");
        }
//        if (StringUtils.isNotEmpty(apiDownloadParam.getHash())) {
//            connect.data("br", apiDownloadParam.getBr());
//        } else {
//            throw new PlugError("请填写下载音乐清晰度");
//        }
        try {
            return new Gson().fromJson(connect.get().body().text(), APIDownloadResult.class);
        } catch (IOException e) {
            log.error("插件运行异常："+e.getMessage());
            throw new PlugError("插件运行io异常");
        }
    }



}
